package com.geccocrawler.gecco.demo.hospital;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.geccocrawler.gecco.annotation.PipelineName;
import com.geccocrawler.gecco.demo.qinhuangdao.Item;
import com.geccocrawler.gecco.pipeline.Pipeline;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: weixiaohu
 * Date: 2019/6/3
 * Time: 15:05
 */
@PipelineName("hospitalPipeline")
public class ProcessPipeline implements Pipeline<SpiderMain> {
    private List allData = Lists.newCopyOnWriteArrayList();
    private int count = 1;
    @Override
    public void process(SpiderMain results) {
        if(count <= AreaPipeline.areaCount){
            List list = results.getList();
            List collect = (List) list.stream().map((item) -> {
                JSONObject jsonObject = JSONUtil.parseObj(item);
                String levelKey = jsonObject.get("aka101", String.class);
                jsonObject.put("aka101", level.get(levelKey));
                String typeKey = jsonObject.get("akb023", String.class);
                jsonObject.put("akb023", type.get(typeKey));
                return jsonObject;
            }).collect(Collectors.toList());
            allData.addAll(collect);
            count++;
        }
        if(count > AreaPipeline.areaCount){
            genExcel(allData);
        }
    }
    Map<String,String> level = Maps.newHashMap();
    Map<String,String> type = Maps.newHashMap();
    {
        level.put("01","三级特等");
	    level.put("02","三级甲等");
	    level.put("03","三级乙等");
	    level.put("04","三级丙等");
	    level.put("05","二级甲等");
	    level.put("06","二级乙等");
	    level.put("07","二级丙等");
	    level.put("08","一级甲等");
	    level.put("09","一级乙等");
	    level.put("10","一级丙等");
	    level.put("11","无等级");

        type.put("10","医院");
        type.put("11","综合医院");
        type.put("12","中医医院");
        type.put("13","中西医结合医院");
        type.put("14","民族医院");
        type.put("15","专科医院");
        type.put("16","疗养院");
        type.put("17","护理院（站）");
        type.put("20","社区卫生服务中心（站）");
        type.put("21","社区卫生服务中心");
        type.put("22","社区卫生服务站");
        type.put("30","卫生院");
        type.put("31","街道卫生院");
        type.put("32","乡镇卫生院");
        type.put("40","门诊部、诊所、医务室、村卫生室");
        type.put("41","门诊部");
        type.put("42","诊所");
        type.put("43","卫生所（室）");
        type.put("44","医务室");
        type.put("45","中小学卫生保健所");
        type.put("46","村卫生室");
        type.put("50","急救中心（站）");
        type.put("70","妇幼保健院（所、站）");
        type.put("71","妇幼保健院");
        type.put("72","妇幼保健所");
        type.put("73","妇幼保健站");
        type.put("74","生殖保健中心");
        type.put("80","专科病防治院（所、站）");
        type.put("81","专科病防治院");
        type.put("82","专科病防治院（站、中心）");
    }

    private void genExcel(List<Item> rows){
        ExcelWriter writer = ExcelUtil.getWriter("d:/spider/定点医疗机构.xlsx");
        writer.addHeaderAlias("aab299", "所属行政区");
        writer.addHeaderAlias("akb021", "医疗机构名称");
        writer.addHeaderAlias("akb020", "医疗机构编码");
        writer.addHeaderAlias("aka101", "医院等级");
        writer.addHeaderAlias("akb023", "医疗机构分类");
        writer.addHeaderAlias("aae006", "地址");
        writer.write(rows, true);
        writer.close();
    }
}
